package com.meelive.ingkee.push.platform.oppo;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.meelive.ingkee.push.InkePushHelper;
import com.meelive.ingkee.push.common.PushUtils;
import com.meelive.ingkee.push.model.InkePushType;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static com.meelive.ingkee.push.model.Constance.TAG;

/**
 * oppo 推送
 * 改动此包名需改动后台的相关配置，否则收不到通知栏点击回调
 */
public class OppoPushDispatcher0 extends Activity {
    /*
     部分机型上，第一次启动一个Activity（Intent.filterEquals）之后，如果使用相同的Intent再次启动，
     只会将应用带到前台，不会传递给Activity:onNewIntent 或者 onCreate。导致了，被通知消息启动的app
     再次点击推送消息的时候，无法跳转到对应界面。
     extras 字段不在Intent.filterEquals中。

     通过Oppo推送通知消息打开的app永远都是 OppoPushDispatcher0 这个，然后再做具体分发。
     所以使用了一系列的alias-activity 来解决。数量足够的alias基本可以保证每次通知消息的Intent是不同的
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            boolean result = fixOrientation();
            Log.i(TAG, "onCreate fixOrientation when Oreo, result = " + result);
        }
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        final Intent intent = getIntent();
        if (intent == null) {
            onFinish();
            return;
        }
        final String data = intent.getStringExtra("data");
        if (PushUtils.DEBUG) {
            Log.d(TAG, "OppoPushDispatcher0 onNotificationMessageClicked is called. " + data);
        }
        InkePushHelper.getInstance().onNotificationClick(this, data, InkePushType.OPPOPUSH);
        onFinish();
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            Log.i(TAG, "avoid calling setRequestedOrientation when Oreo.");
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    public void onFinish() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        });
    }

    // 解决8.0崩溃问题
    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }

    private boolean fixOrientation() {
        try {
            Field field = Activity.class.getDeclaredField("mActivityInfo");
            field.setAccessible(true);
            ActivityInfo o = (ActivityInfo) field.get(this);
            o.screenOrientation = -1;
            field.setAccessible(false);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
